//package com.tianma315.wx.service.impl;
//
//import com.tianma315.core.exception.MessageException;
//import com.tianma315.core.mapper.RoleMapper;
//import com.tianma315.core.mapper.UserMapper;
//import com.tianma315.core.model.pojo.Role;
//import com.tianma315.core.model.pojo.User;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.social.security.SocialUser;
//
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//
//
//public class UserDetailsServiceImpl implements UserDetailsService{
//    @Autowired
//    private UserMapper userMapper;
//    @Autowired
//    private RoleMapper roleMapper;
//
//    @Override
//    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
//        User user = userMapper.selectEnableByUsername(s);
//        if (user == null)
//            throw new UsernameNotFoundException("您登录的账号不存在或已被冻结");
//
//        //
//        List<Role> roles = roleMapper.selectByUserId(user.getUser_id());
//        Collection<GrantedAuthority> authorities = new ArrayList<>();
//        if (roles != null) {
//            for (Role role : roles) {
//                authorities.add(role::getRole_name);
//            }
//        }
//
//        //判断当前账号是否是商户号
//        boolean isMerchant = false;
//        for (Role role : roles) {
//            if ("ROLE_WAREHOUSE".equals(role.getRole_name())) {
//                isMerchant = true;
//                break;
//            }
//        }
//        if (!isMerchant)
//            throw new MessageException("您登录的账号不是发货员");
//
//        return new SocialUser(user.getUsername(), user.getPassword(), authorities);
//    }
//}
